﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TrieUtils
{
    public class TrieCollection<T> : ICollection<T>
    {
        private Trie<T> _rootTrie = new Trie<T>();

        public TrieCollection() { }
        public TrieCollection(IEnumerable<T> items)
        {
            AddRange(items);
        }

        public void AddRange(IEnumerable<T> items)
        {
            foreach (var item in items)
            {
                Add(item);
            }
        }

        public IEnumerable<T> FindItems(string pattern, int limit = int.MaxValue)
        {
            return _rootTrie.FindItems(pattern, limit);
        }

        #region ICollection<T> Members

        public int Count { get; private set; }

        public bool IsReadOnly { get; set; }

        public void Add(T item)
        {
            ReadOnlyCheck();
            Count++;
            _rootTrie.Add(item);
        }

        public void Clear()
        {
            ReadOnlyCheck();
            Count = 0;
            _rootTrie = new Trie<T>();
        }

        public bool Contains(T item)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }
        public bool Remove(T item)
        {
            ReadOnlyCheck();
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion

        private void ReadOnlyCheck()
        {
            if (IsReadOnly)
                throw new NotSupportedException("The collection is ReadOnly and cannot be modified.");
        }
    }
}
